package com.fxc.impl;

import com.fxc.api.ProjectService;
import com.fxc.entity.PO.MemberConfirmInfoPO;
import com.fxc.entity.PO.MemberLaunchInfoPO;
import com.fxc.entity.PO.ProjectPO;
import com.fxc.entity.PO.ReturnPO;
import com.fxc.entity.VO.*;
import com.fxc.mapper.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author FXC
 * @version 1.0
 * @date 2021/12/10 14:58
 */
@Transactional(readOnly = true)
@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ReturnPOMapper returnPOMapper;

    @Resource
    private MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;

    @Resource
    private MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;

    @Resource
    private ProjectPOMapper projectPOMapper;

    @Resource
    private ProjectItemPicPOMapper projectItemPicPOMapper;

    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveProject(ProjectVO projectVO, Integer memberId) {

        // 一保存ProjectVO对象
        // 1.创建空的ProjectVO对象
        ProjectPO projectPO = new ProjectPO();

        // 2.把ProjectVO中属性复制到projectPO中
        BeanUtils.copyProperties(projectVO, projectPO);

        // 修复bug：把memberId设置到projectPO中
        projectPO.setMemberid(memberId);

        // 生成创建时间存入
        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        projectPO.setCreatedate(createDate);

        //结束时间
        Integer day = projectVO.getDay();

        LocalDate now = LocalDate.now();

        LocalDate localDate = now.plusDays(day);

        projectPO.setDeploydate(localDate.toString());


        //一些数字设置随机值
        projectPO.setSupporter((int) (Math.random() * 99));
        projectPO.setSupportmoney((long) (Math.random()* projectPO.getMoney()));
        projectPO.setFollower((int) (Math.random() * 99));

        // status：设置成0，即将开始
        projectPO.setStatus(0);

        // 3.保存ProjectPO
        // 为了能够获取到ProjectPO保存后的自增主键，需要到ProjectPOMapper.xml文件中进行相关配置
        // <insert id="insertSelective" useGeneratedKeys="true" keyProperty="id" parameterType="com.fxc.entity.PO.ProjectPO">
        projectPOMapper.insertSelective(projectPO);

        // 4.从ProjectPO对象这里获取自增主键

        Integer projectId = projectPO.getId();

        // 二、保存项目，分类的关联信息
        // 2.1从projectVO对象中获取typeIdList
        List<Integer> typeIdList = projectVO.getTypeIdList();
        projectPOMapper.insertTypeRelationship(typeIdList, projectId);

        // 三、保存项目、标签关联关系信息
        List<Integer> tagIdList = projectVO.getTagIdList();
        projectPOMapper.insertTagRelationship(tagIdList, projectId);

        // 四、保存项目中、详情图片的路径信息
        List<String> detailPicturePathList = projectVO.getDetailPicturePathList();
        projectItemPicPOMapper.insertPathList(projectId, detailPicturePathList);

        // 五、保存项目发起人信息
        MemberLauchInfoVO memberLauchInfoVO = projectVO.getMemberLauchInfoVO();
        MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();
        BeanUtils.copyProperties(memberLauchInfoVO, memberLaunchInfoPO);
        memberLaunchInfoPO.setMemberid(memberId);

        memberLaunchInfoPOMapper.insert(memberLaunchInfoPO);

        // 六、保存项目汇报的信息
        List<ReturnVO> returnVOList = projectVO.getReturnVOList();
        List<ReturnPO> returnPOList = new ArrayList<>();

        for (ReturnVO returnVO : returnVOList){

            ReturnPO returnPO = new ReturnPO();

            BeanUtils.copyProperties(returnVO, returnPO);
            returnPO.setSignalpurchase(1);
            returnPOList.add(returnPO);
        }

        returnPOMapper.insertReturnPOBatch(returnPOList, projectId);

        // 七、保存项目的确认信息
        MemberConfirmInfoVO memberConfirmInfoVO = projectVO.getMemberConfirmInfoVO();
        MemberConfirmInfoPO memberConfirmInfoPO = new MemberConfirmInfoPO();
        BeanUtils.copyProperties(memberConfirmInfoVO, memberConfirmInfoPO);
        memberConfirmInfoPO.setMemberid(memberId);
        memberConfirmInfoPOMapper.insert(memberConfirmInfoPO);

    }

    @Override
    public List<PortalTypeVO> getPortalTypeVO() {

        return projectPOMapper.selectPortalTypeVOList();
    }

    @Override
    public DetailProjectVO getDetailProjectVO(Integer projectId) {

        // 查询得到DetailProjectVO对象
        DetailProjectVO detailProjectVO = projectPOMapper.selectDetailProjectVO(projectId);

        // 2.根据status确定statusText
        Integer status = detailProjectVO.getStatus();

        switch (status){
            case 0:
                detailProjectVO.setStatusText("审核中");
                break;
            case 1:
                detailProjectVO.setStatusText("众筹中");
                break;
            case 2:
                detailProjectVO.setStatusText("众筹成功");
                break;
            case 3:
                detailProjectVO.setStatusText("已关闭");
                break;
            default:
                break;
        }

        // 3.根据deployDate计算lastDay
        String deployDate = detailProjectVO.getDeployDate();

        // 现在的日期
        LocalDate now = LocalDate.now();

        // 结束的日期
        LocalDate parse = LocalDate.parse(deployDate);

        // 进行比较得出还剩多少天结束
        Integer lastDay = (int) now.until(parse, ChronoUnit.DAYS);

        detailProjectVO.setLastDay(lastDay);

        return detailProjectVO;
    }
}
